package com.sinochem.yunlian.ship.base.rest;

import com.sinochem.yunlian.ship.base.biz.BsDataDictBiz;
import com.sinochem.yunlian.ship.base.biz.BsDataDictItemBiz;
import com.sinochem.yunlian.ship.base.entity.BsDataDict;
import com.sinochem.yunlian.ship.base.entity.BsDataDictItem;
import com.sinochem.yunlian.ship.base.vo.response.DictItemVo;
import com.sinochem.yunlian.ship.common.constant.DisableStatus;
import com.sinochem.yunlian.ship.common.constant.RedisKey;
import com.sinochem.yunlian.ship.common.enums.ResponseCode;
import com.sinochem.yunlian.ship.common.msg.RestResponse;
import com.sinochem.yunlian.ship.common.rest.BaseController;
import com.sinochem.yunlian.ship.common.util.DeletStatusUtil;
import com.sinochem.yunlian.ship.common.util.MapUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @author cjw
 * @date 2018-03-06 13:45
 *
 * */
@RestController
@RequestMapping("/ydict")
public class YYDataDictController extends BaseController<BsDataDictItemBiz,BsDataDictItem> {
    private Logger logger = LoggerFactory.getLogger(YYDataDictController.class);

    @Autowired
    private BsDataDictItemBiz bsDataDictItemBiz;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private BsDataDictBiz dataDictBiz;


    @RequestMapping(value = "/dictList",method = RequestMethod.GET)
    public RestResponse clearCache() {
        List<BsDataDict> dicts = dataDictBiz.getAllDicts();
        return RestResponse.success(dicts);
    }
    @RequestMapping("dictDetail")
    public RestResponse getDictDetail(String dictCode) {

        List<DictItemVo> resultList =  new ArrayList<>();

        List<BsDataDictItem> items = bsDataDictItemBiz.findByDictCodeForOM(dictCode);
        for (BsDataDictItem item : items) {
            DictItemVo dictItemVo = new DictItemVo();
            dictItemVo.setDictId(item.getDictId());
            dictItemVo.setDictItemId(item.getId());
            dictItemVo.setDictCode(item.getDictCode());
            dictItemVo.setItemValue(item.getItemValue());
            dictItemVo.setItemCode(item.getItemCode());
            dictItemVo.setItemName(item.getItemName());
            dictItemVo.setShowOrder(item.getShowOrder());
            dictItemVo.setDisableStatus(item.getDisableStatus());
            resultList.add(dictItemVo);
        }
        return RestResponse.success(resultList);
    }
    @RequestMapping(value = "/modifyItem",method = RequestMethod.POST)
    public RestResponse modifyItem(@RequestBody  DictItemVo item) {
        //0 insert,1 update
        String key = String.format(RedisKey.KEY_DICT_ITEM_LIST_PREFIX,item.getDictCode());
        String key1 = String.format(RedisKey.KEY_DICT_ITEM_MAP_PREFIX,item.getDictCode());

        if(item.getType()==0){
            HashMap map = new HashMap();
            map.put("dictCode",item.getDictCode());
            map.put("itemValue",item.getItemValue());
            String itemName = bsDataDictItemBiz.getItemNameByItemValueByMap(map);
            if(itemName!=null){
                return RestResponse.response(ResponseCode.NOTIFICATION.getCode(),"字典值重复");
            }
            BsDataDictItem im = new BsDataDictItem();
            im.setShowOrder(item.getShowOrder());
            im.setItemName(item.getItemName());
            im.setItemCode(item.getItemCode());
            im.setDictCode(item.getDictCode());
            im.setDictId(item.getDictId());
            im.setCreateTime(new Date());
            im.setDeleteStatus(DeletStatusUtil.NO_DELETE);
            im.setItemValue(item.getItemValue());
            im.setDisableStatus(DisableStatus.STATUS_ENABLE);
            bsDataDictItemBiz.insert(im);
        }else{
            BsDataDictItem item_old = bsDataDictItemBiz.selectById(item.getDictItemId());
            item_old.setItemName(item.getItemName());
            item_old.setItemCode(item.getItemCode());
            item_old.setShowOrder(item.getShowOrder());
            item_old.setDisableStatus(item.getDisableStatus());
            bsDataDictItemBiz.updateById(item_old);
        }
        //刷新
        redisTemplate.delete(key);
        redisTemplate.delete(key1);
        if(MapUtil.hasKey(key)){
            redisTemplate.delete(key);
        }
        return RestResponse.success();
    }
    @RequestMapping(value = "/cleanCache",method = RequestMethod.GET)
    public RestResponse clearCache(String cacheCode) {
        logger.info("刷新缓存>>>>>>>>>>>>>>Key：{}",cacheCode);
        String key = String.format(RedisKey.KEY_DICT_ITEM_LIST_PREFIX,cacheCode);
        String key1 = String.format(RedisKey.KEY_DICT_ITEM_MAP_PREFIX,cacheCode);

        redisTemplate.delete(key);
        redisTemplate.delete(key1);
        if(MapUtil.hasKey(cacheCode)){
            redisTemplate.delete(cacheCode);
        }

        //货品
        if(cacheCode.contains("category")){
            logger.info("刷新缓存货品{}",cacheCode);

            redisTemplate.delete(RedisKey.KEY_MATERIAL_CATEGORY_MAP);
            redisTemplate.delete(RedisKey.KEY_MATERIAL_CATEGORY_ALL);

            Set<String> set = redisTemplate.keys("base:key:category:level:"+"*");
            Iterator<String> it = set.iterator();
            while(it.hasNext()){
                String keyStr = it.next();
                logger.info("刷新缓存货品key：{}",keyStr);
                redisTemplate.delete(keyStr);
            }
        }
        //港口
        if(cacheCode.contains("port")){
            logger.info("刷新缓存港口",cacheCode);
            redisTemplate.delete(RedisKey.KEY_PORT_MAP);
        }
        //app版本
        if(cacheCode.contains("version")){
            logger.info("app版本{}",cacheCode);
            Set<String> set = redisTemplate.keys("base:key:version:"+"*");
            Iterator<String> it = set.iterator();
            while(it.hasNext()){
                String keyStr = it.next();
                logger.info("pp版本key：{}",keyStr);
                redisTemplate.delete(keyStr);
            }
        }
        return RestResponse.success();
    }

}